home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / add-log.el < prev    next >
Lisp/Scheme  |  1993-07-03  |  15KB  |  412 lines

  1. ;;; add-log.el --- change log maintenance commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: maint
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This facility is documented in the Emacs Manual.
  26.  
  27. ;;; Code:
  28.  
  29. ;;;###autoload
  30. (defvar change-log-default-name nil
  31.   "*Name of a change log file for \\[add-change-log-entry].")
  32.  
  33. (defun change-log-name ()
  34.   (or change-log-default-name
  35.       (if (eq system-type 'vax-vms) "$CHANGE_LOG$.TXT" "ChangeLog")))
  36.  
  37. (defun prompt-for-change-log-name ()
  38.   "Prompt for a change log name."
  39.   (let ((default (change-log-name)))
  40.     (expand-file-name
  41.      (read-file-name (format "Log file (default %s): " default)
  42.              nil default))))
  43.  
  44. ;;;###autoload
  45. (defun find-change-log (&optional file-name)
  46.   "Find a change log file for \\[add-change-log-entry] and return the name.
  47. Optional arg FILE-NAME is a name to try first.
  48. If FILE-NAME is nil, use the value of `change-log-default-name' if non-nil.
  49. Failing that, use \"ChangeLog\" in the current directory.
  50. If the file does not exist in the named directory, successive parent
  51. directories are tried.
  52.  
  53. Once a file is found, `change-log-default-name' is set locally in the
  54. current buffer to the complete file name."
  55.   (or file-name
  56.       (setq file-name (or change-log-default-name
  57.               ;; Chase links in the source file
  58.               ;; and use the change log in the dir where it points.
  59.               (and buffer-file-name
  60.                    (file-name-directory
  61.                 (file-chase-links buffer-file-name)))
  62.               default-directory)))
  63.   (if (and (eq file-name change-log-default-name)
  64.        (assq 'change-log-default-name (buffer-local-variables)))
  65.       ;; Don't do the searching if we already have a buffer-local value.
  66.       file-name
  67.  
  68.     (if (file-directory-p file-name)
  69.     (setq file-name (expand-file-name (change-log-name) file-name)))
  70.     ;; Chase links before visiting the file.
  71.     ;; This makes it easier to use a single change log file
  72.     ;; for several related directories.
  73.     (setq file-name (file-chase-links file-name))
  74.     (setq file-name (expand-file-name file-name))
  75.     ;; Move up in the dir hierarchy till we find a change log file.
  76.     (let ((file1 file-name)
  77.       parent-dir)
  78.       (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
  79.           (progn (setq parent-dir
  80.                    (file-name-directory
  81.                 (directory-file-name
  82.                  (file-name-directory file1))))
  83.              ;; Give up if we are already at the root dir.
  84.              (not (string= (file-name-directory file1)
  85.                        parent-dir))))
  86.     ;; Move up to the parent dir and try again.
  87.     (setq file1 (expand-file-name (change-log-name) parent-dir)))
  88.       ;; If we found a change log in a parent, use that.
  89.       (if (or (get-file-buffer file1) (file-exists-p file1))
  90.       (setq file-name file1)))
  91.     ;; Make a local variable in this buffer so we needn't search again.
  92.     (set (make-local-variable 'change-log-default-name) file-name)
  93.     file-name))
  94.  
  95. ;;;###autoload
  96. (defun add-change-log-entry (&optional whoami file-name other-window)
  97.   "Find change log file and add an entry for today.
  98. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  99. Second arg is file name of change log.  If nil, uses `change-log-default-name'.
  100. Third arg OTHER-WINDOW non-nil means visit in other window."
  101.   (interactive (list current-prefix-arg
  102.              (prompt-for-change-log-name)))
  103.   (let* ((full-name (if whoami
  104.             (read-input "Full name: " (user-full-name))
  105.               (user-full-name)))
  106.      ;; Note that some sites have room and phone number fields in
  107.      ;; full name which look silly when inserted.  Rather than do
  108.      ;; anything about that here, let user give prefix argument so that
  109.      ;; s/he can edit the full name field in prompter if s/he wants.
  110.      (login-name (if whoami
  111.              (read-input "Login name: " (user-login-name))
  112.                (user-login-name)))
  113.      (site-name (if whoami
  114.             (read-input "Site name: " (system-name))
  115.               (system-name)))
  116.      (defun (add-log-current-defun))
  117.      paragraph-end entry)
  118.  
  119.     (setq file-name (find-change-log file-name))
  120.  
  121.     ;; Set ENTRY to the file name to use in the new entry.
  122.     (and buffer-file-name
  123.      ;; Never want to add a change log entry for the ChangeLog file itself.
  124.      (not (string= buffer-file-name file-name))
  125.      (setq entry (if (string-match
  126.               (concat "^" (regexp-quote (file-name-directory
  127.                              file-name)))
  128.               buffer-file-name)
  129.              (substring buffer-file-name (match-end 0))
  130.                (file-name-nondirectory buffer-file-name))))
  131.  
  132.     (if (and other-window (not (equal file-name buffer-file-name)))
  133.     (find-file-other-window file-name)
  134.       (find-file file-name))
  135.     (undo-boundary)
  136.     (goto-char (point-min))
  137.     (if (looking-at (concat (regexp-quote (substring (current-time-string)
  138.                              0 10))
  139.                 ".* " (regexp-quote full-name)
  140.                 "  (" (regexp-quote login-name) "@"))
  141.     (forward-line 1)
  142.       (insert (current-time-string)
  143.           "  " full-name
  144.           "  (" login-name "@" site-name ")\n\n"))
  145.  
  146.     ;; Search only within the first paragraph.
  147.     (if (looking-at "\n*[^\n* \t]")
  148.     (skip-chars-forward "\n")
  149.       (forward-paragraph 1))
  150.     (setq paragraph-end (point))
  151.     (goto-char (point-min))
  152.  
  153.     ;; Now insert the new line for this entry.
  154.     (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
  155.        ;; Put this file name into the existing empty entry.
  156.        (if entry
  157.            (insert entry)))
  158.       ((and (re-search-forward
  159.          (concat (regexp-quote (concat "* " entry))
  160.              ;; Don't accept `foo.bar' when
  161.              ;; looking for `foo':
  162.              "\\(\\s \\|[(),:]\\)")
  163.          paragraph-end t))
  164.        ;; Add to the existing entry for the same file.
  165.        (re-search-forward "^\\s *$\\|^\\s \\*")
  166.        (beginning-of-line)
  167.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  168.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  169.        (insert "\n\n")
  170.        (forward-line -2)
  171.        (indent-relative-maybe))
  172.       (t
  173.        ;; Make a new entry.
  174.        (forward-line 1)
  175.        (while (looking-at "\\sW")
  176.          (forward-line 1))
  177.        (while (and (not (eobp)) (looking-at "^\\s *$"))
  178.          (delete-region (point) (save-excursion (forward-line 1) (point))))
  179.        (insert "\n\n\n")
  180.        (forward-line -2)
  181.        (indent-to left-margin)
  182.        (insert "* " (or entry ""))))
  183.     ;; Now insert the function name, if we have one.
  184.     ;; Point is at the entry for this file,
  185.     ;; either at the end of the line or at the first blank line.
  186.     (if defun
  187.     (progn
  188.       ;; Make it easy to get rid of the function name.
  189.       (undo-boundary)
  190.       (insert (if (save-excursion
  191.             (beginning-of-line 1)
  192.             (looking-at "\\s *$")) 
  193.               ""
  194.             " ")
  195.           "(" defun "): "))
  196.       ;; No function name, so put in a colon unless we have just a star.
  197.       (if (not (save-excursion
  198.          (beginning-of-line 1)
  199.          (looking-at "\\s *\\(\\*\\s *\\)?$")))
  200.       (insert ": ")))))
  201.  
  202. ;;;###autoload
  203. (defun add-change-log-entry-other-window (&optional whoami file-name)
  204.   "Find change log file in other window and add an entry for today.
  205. First arg (interactive prefix) non-nil means prompt for user name and site.
  206. Second arg is file name of change log.
  207. Interactively, with a prefix argument, the file name is prompted for."
  208.   (interactive (if current-prefix-arg
  209.            (list current-prefix-arg
  210.              (prompt-for-change-log-name))))
  211.   (add-change-log-entry whoami file-name t))
  212. ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
  213.  
  214. ;;;###autoload
  215. (defun change-log-mode ()
  216.   "Major mode for editing change logs; like Indented Text Mode.
  217. Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
  218. New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
  219. Each entry behaves as a paragraph, and the entries for one day as a page.
  220. Runs `change-log-mode-hook'."
  221.   (interactive)
  222.   (kill-all-local-variables)
  223.   (indented-text-mode)
  224.   (setq major-mode 'change-log-mode
  225.     mode-name "Change Log"
  226.     left-margin 8
  227.     fill-column 74)
  228.   ;; Let each entry behave as one paragraph:
  229.   (set (make-local-variable 'paragraph-start) "^\\s *$\\|^^L")
  230.   (set (make-local-variable 'paragraph-separate) "^\\s *$\\|^^L\\|^\\sw")
  231.   ;; Let all entries for one day behave as one page.
  232.   ;; Match null string on the date-line so that the date-line
  233.   ;; is grouped with what follows.
  234.   (set (make-local-variable 'page-delimiter) "^\\<\\|^ ")
  235.   (set (make-local-variable 'version-control) 'never)
  236.   (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
  237.   (run-hooks 'change-log-mode-hook))
  238.  
  239. (defvar add-log-current-defun-header-regexp
  240.   "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[a-z_---A-Z]+\\)[ \t]*[:=]"
  241.   "*Heuristic regexp used by `add-log-current-defun' for unknown major modes.")
  242.  
  243. (defun add-log-current-defun ()
  244.   "Return name of function definition point is in, or nil.
  245.  
  246. Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
  247. Texinfo (@node titles), and Fortran.
  248.  
  249. Other modes are handled by a heuristic that looks in the 10K before
  250. point for uppercase headings starting in the first column or
  251. identifiers followed by `:' or `=', see variable
  252. `add-log-current-defun-header-regexp'.
  253.  
  254. Has a preference of looking backwards."
  255.   (condition-case nil
  256.       (save-excursion
  257.     (let ((location (point)))
  258.       (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode))
  259.          ;; If we are now precisely a the beginning of a defun,
  260.          ;; make sure beginning-of-defun finds that one
  261.          ;; rather than the previous one.
  262.          (or (eobp) (forward-char 1))
  263.          (beginning-of-defun)
  264.          ;; Make sure we are really inside the defun found, not after it.
  265.          (if (and (progn (end-of-defun)
  266.                  (< location (point)))
  267.               (progn (forward-sexp -1)
  268.                  (>= location (point))))
  269.              (progn
  270.                (if (looking-at "\\s(")
  271.                (forward-char 1))
  272.                (forward-sexp 1)
  273.                (skip-chars-forward " ")
  274.                (buffer-substring (point)
  275.                      (progn (forward-sexp 1) (point))))))
  276.         ((and (memq major-mode '(c-mode 'c++-mode))
  277.               (save-excursion (beginning-of-line)
  278.                       ;; Use eq instead of = here to avoid
  279.                       ;; error when at bob and char-after
  280.                       ;; returns nil.
  281.                       (while (eq (char-after (- (point) 2)) ?\\)
  282.                     (forward-line -1))
  283.                       (looking-at "[ \t]*#[ \t]*define[ \t]")))
  284.          ;; Handle a C macro definition.
  285.          (beginning-of-line)
  286.          (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
  287.            (forward-line -1))
  288.          (search-forward "define")
  289.          (skip-chars-forward " \t")
  290.          (buffer-substring (point)
  291.                    (progn (forward-sexp 1) (point))))
  292.         ((memq major-mode '(c-mode 'c++-mode))
  293.          (beginning-of-line)
  294.          ;; See if we are in the beginning part of a function,
  295.          ;; before the open brace.  If so, advance forward.
  296.          (while (not (looking-at "{\\|\\(\\s *$\\)"))
  297.            (forward-line 1))
  298.          (or (eobp)
  299.              (forward-char 1))
  300.          (beginning-of-defun)
  301.          (if (progn (end-of-defun)
  302.                 (< location (point)))
  303.              (progn
  304.                (backward-sexp 1)
  305.                (let (beg tem)
  306.  
  307.              (forward-line -1)
  308.              ;; Skip back over typedefs of arglist.
  309.              (while (and (not (bobp))
  310.                      (looking-at "[ \t\n]"))
  311.                (forward-line -1))
  312.              ;; See if this is using the DEFUN macro used in Emacs,
  313.              ;; or the DEFUN macro used by the C library.
  314.              (if (condition-case nil
  315.                  (and (save-excursion
  316.                     (forward-line 1)
  317.                     (backward-sexp 1)
  318.                     (beginning-of-line)
  319.                     (setq tem (point))
  320.                     (looking-at "DEFUN\\b"))
  321.                       (>= location tem))
  322.                    (error nil))
  323.                  (progn
  324.                    (goto-char tem)
  325.                    (down-list 1)
  326.                    (if (= (char-after (point)) ?\")
  327.                    (progn
  328.                      (forward-sexp 1)
  329.                      (skip-chars-forward " ,")))
  330.                    (buffer-substring (point)
  331.                          (progn (forward-sexp 1) (point))))
  332.                ;; Ordinary C function syntax.
  333.                (setq beg (point))
  334.                (if (condition-case nil
  335.                    ;; Protect against "Unbalanced parens" error.
  336.                    (progn
  337.                      (down-list 1) ; into arglist
  338.                      (backward-up-list 1)
  339.                      (skip-chars-backward " \t")
  340.                      t)
  341.                  (error nil))
  342.                    ;; Verify initial pos was after
  343.                    ;; real start of function.
  344.                    (if (and (save-excursion
  345.                       (goto-char beg)
  346.                       ;; For this purpose, include the line
  347.                       ;; that has the decl keywords.  This
  348.                       ;; may also include some of the
  349.                       ;; comments before the function.
  350.                       (while (and (not (bobp))
  351.                               (save-excursion
  352.                             (forward-line -1)
  353.                             (looking-at "[^\n\f]")))
  354.                         (forward-line -1))
  355.                       (>= location (point)))
  356.                     ;; Consistency check: going down and up
  357.                     ;; shouldn't take us back before BEG.
  358.                     (> (point) beg))
  359.                    (buffer-substring (point)
  360.                              (progn (backward-sexp 1)
  361.                                 (point))))))))))
  362.         ((memq major-mode
  363.                '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
  364.                   plain-tex-mode latex-mode;; cmutex.el
  365.                   ))
  366.          (if (re-search-backward
  367.               "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
  368.              (progn
  369.                (goto-char (match-beginning 0))
  370.                (buffer-substring (1+ (point));; without initial backslash
  371.                      (progn
  372.                        (end-of-line)
  373.                        (point))))))
  374.         ((eq major-mode 'texinfo-mode)
  375.          (if (re-search-backward "^@node[ \t]+\\([^,]+\\)," nil t)
  376.              (buffer-substring (match-beginning 1)
  377.                        (match-end 1))))
  378.                 ((eq major-mode 'fortran-mode)
  379.                  ;; must be inside function body for this to work
  380.                  (beginning-of-fortran-subprogram)
  381.                  (let ((case-fold-search t)) ; case-insensitive
  382.                    ;; search for fortran subprogram start
  383.                    (if (re-search-forward
  384.              "^[ \t]*\\(program\\|subroutine\\|function\
  385. \\|[ \ta-z0-9*]*[ \t]+function\\)"
  386.              nil t)
  387.                        (progn
  388.                          ;; move to EOL or before first left paren
  389.                          (if (re-search-forward "[(\n]" nil t)
  390.                  (progn (forward-char -1)
  391.                     (skip-chars-backward " \t"))
  392.                (end-of-line))
  393.              ;; Use the name preceding that.
  394.                          (buffer-substring (point)
  395.                                            (progn (forward-sexp -1)
  396.                                                   (point)))))))
  397.         (t
  398.          ;; If all else fails, try heuristics
  399.          (let (case-fold-search)
  400.            (end-of-line)
  401.            (if (re-search-backward add-log-current-defun-header-regexp
  402.                        (- (point) 10000)
  403.                        t)
  404.                (buffer-substring (match-beginning 1)
  405.                      (match-end 1))))))))
  406.     (error nil)))
  407.  
  408.  
  409. (provide 'add-log)
  410.  
  411. ;;; add-log.el ends here
  412.